package com.arvin.router.core.thread;

import android.icu.text.UFormat;
import android.icu.util.TimeUnit;
import android.print.PrintJob;
import android.support.annotation.NonNull;
import android.text.style.TtsSpan;
import android.view.ViewTreeObserver;

import java.util.FormatFlagsConversionMismatchException;
import java.util.PropertyResourceBundle;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

public class DefaultPoolExecutor {
    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);
        @Override
        public Thread newThread(@NonNull Runnable r) {
            return new Thread(r,"DNRouter # " + mCount.getAndIncrement());
        }
    };

    private final static int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private final static int MAX_CORE_POOL_SIZE = CPU_COUNT + 1;
    private static final long SURPLUS_THREAD_LIFE = 30L;

    public static ThreadPoolExecutor newDefaultPoolExecutor(int corePoolSize){
        if (corePoolSize == 0){
            return null;
        }
        corePoolSize= Math.min(corePoolSize,MAX_CORE_POOL_SIZE);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, corePoolSize, SURPLUS_THREAD_LIFE,
                java.util.concurrent.TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(64),
                sThreadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        return threadPoolExecutor;
    }
}
